from src.util.Route import *
from src.service import FileManageService


# 添加文件或文件夹
@Post('/file/add', name='添加文件')
def addFile():
    file_type = request.args.get('type')
    parent = request.args.get('parent')
    cover = request.args.get('cover') == 'Y'
    # 检查文件类型
    # 文件
    if file_type == 'file':
        file = request.files.get('file')
        r = FileManageService.addFile(int(parent), getUsername(), file, cover)
        if r is False:
            return R.bad_request("创建失败")
        elif type(r) == str:
            return R.bad_request(r)
        return R.success('ok', '操作成功')
    # 文件夹
    elif file_type == 'dir':
        name = request.args.get('name')
        if not name:
            return R.bad_request('需要名称')
        r = FileManageService.addDir(int(parent), getUsername(), name, cover)
        if r is False:
            return R.bad_request("创建失败")
        # 返回服务信息
        elif type(r) == str:
            return R.bad_request(r)
        return R.success('ok', '操作成功')
    return R.bad_request("请选择文件类型")


# 获取文件列表
@Post('/file/list', name='获取文件列表')
def getList():
    param = formatSelectInfo()
    r = FileManageService.getFileList(*param)
    return R.success(r, 'ok')


@Delete('/file', name='删除文件夹或文件')
def delFile():
    ids: list[int] = request.json
    r = FileManageService.delFile(ids)
    if r is True:
        return R.success(msg='删除成功')
    return R.bad_request(f"删除失败 {r}")


# 获取文件权限
@Get('/file/auth', name='获取文件权限')
def getFileAuth():
    f_id = request.args.get('id')
    if not f_id:
        return R.bad_request('找不到参数id')
    a_type = request.args.get('type')
    r = FileManageService.getFileAuthList(int(f_id), a_type)
    return R.success(r)


# 文件扫描到数据库
@Get('/file/scan', name='文件扫描')
def fileScan():
    f_id = request.args.get('id')
    if not f_id:
        return R.bad_request('找不到参数id')
    r = FileManageService.scanDir(int(f_id), getUsername())
    if r:
        return R.success(msg="扫描完成")
    return R.bad_request("所选不是文件或目录不存在")


@Put('/file/auth', name="添加文件权限")
def addAuth():
    data = request.json
    f_id = request.args.get('id')
    if not f_id:
        return R.bad_request('找不到参数id')
    r = FileManageService.addFileAuth(int(f_id), data, getUsername())
    if r:
        return R.success(msg="添加成功")
    return R.bad_request('操作失败')


@Delete('/file/auth', name="删除文件某权限")
def delAuth():
    aIds: list[int] = request.json
    fId = request.args.get('id')
    r = FileManageService.delFileAuth(int(fId), aIds, getUsername())
    if r is True:
        return R.success(msg="操作成功")
    return R.bad_request(f"操作失败 {r}")


@Post('/file/auth', name="修改文件权限")
def updateFileAuth():
    data = request.json
    f_id = request.args.get('id')
    if not f_id:
        return R.bad_request('找不到参数id')
    r = FileManageService.updateFileAuth(int(f_id), data, getUsername())
    if r is True:
        return R.success(msg="操作成功")
    return R.bad_request(f"操作失败 {r}")


# 获取父文件列表
@Get("/file/parents", name="获取某文件父路径文件列表")
def getFileParentList():
    f_id = request.args.get('id')
    if not f_id:
        return R.bad_request('找不到参数id')
    r = FileManageService.getFileParentList(int(f_id))
    return R.success(r)


# 获取文件夹大小
@Get("/file/size", name="计算文件夹大小")
def calculateFolderSize():
    f_id = request.args.get('id')
    if not f_id:
        return R.bad_request('找不到参数id')
    r = FileManageService.getFolderSize(int(f_id))
    return R.success(r)


@Get("/file/auth/change/extend", name='修改文件是否需要继承')
def changeFileExtend():
    fId = request.args.get("id")
    extend = request.args.get("extend")
    if not fId:
        return R.bad_request('找不到参数id')
    r = FileManageService.changeFileExtend(int(fId), extend, getUsername())
    if r is True:
        return R.success(msg="操作成功")
    return R.bad_request(f"操作失败 {r}")


@Get("/file/rename", name="重命名文件")
def fileRename():
    fId = request.args.get("id")
    fileName = request.args.get("name")

# @Post("/file/upload", name="文件上传")
# def updateFileAuthExtend():
#     return
